/**
 * The Mask user-interface component class
 * @class syslib.ui.Mask
 * @author Gabriel Ricci
 */
syslib.ui.MaskInput = __class(syslib.ui.VisualTag, 
/**
 * @lends syslib.ui.Mask
 */
{
	prepare : function(){
		// TAG ATTRIBUTES
		var attribs = this.__attributes;

		// CLASS ATTRIBUTES
		this.addProperty('string', 'mask', attribs.mask);
		this.addProperty('string', 'id', (__valid(attribs.id)) ? attribs.id : 'maskinput_' + Math.floor(Math.random() * 101));
		this.addProperty('string', 'direction', (__valid(attribs.maskdirection)) ? attribs.maskdirection : 'ltr');

		// SETS
		this.__attributes.id = this.getId();
		
		// RETURN
		return true;
	},

	execute : function(){
		// JSON STRUCTURE
		var elemtJSON 	= ["input", this.__attributes];

		// CREATE ELEMENT
		var mask = this.createDOM(elemtJSON);
		
		// RETURN
		return mask;
	},

	finalize : function(){
		// this.__element._withoutmask = '';
		syslib.event.listen('keypress', this.__element, this.applyMask.applyTo(this), true);
	},

	applyMask : function(evt){
		var element 	= this.__element;
		var mask		= this.getMask();
		var direction	= this.getDirection();
		var str 		= element.value;
		var origStr 	= element.value;
		var code		= window.event ? window.event.keyCode : evt.which;
		var ch			= syslib.util.fromKeyCode(code);
		var curMask		= null;

		if(syslib.ui.MaskInput.isSpecialCode(code) && evt.shiftKey === false){return true;}
		if(evt.ctrlKey === false && evt.altKey === false){
			if(str.length >= mask.length){
				syslib.event.stopPropagation(evt);
				return false;
			}

			if(direction !== "ltr"){
				for(var i = 0; i < mask.length; i++){
					var maskCh = mask.charAt(i);
					if("$#%*".indexOf(maskCh) === -1){
						str = syslib.string.replace(maskCh, "", str);
					}
				}

				var reversedStr 		= str.reverse();
				var reversedMask	= mask.reverse();
				str							= "";

				var maskIndex = 1;
				for(var c = 0; c <= reversedMask.length; c++){
					var maskCh 	= reversedMask.charAt((c + maskIndex));
					var strCh	= reversedStr.charAt(c);

					if(maskCh !== "#" && maskCh !== "$" && maskCh !== "%" && maskCh !== "*"){
						if(__valid(strCh)){
							str = str + maskCh;
							str = str + strCh;
							maskIndex++;
						}
					} else {
						curMask = maskCh;
						if(!__valid(strCh)){
							break;
						} else {
							str = str + strCh;
						}
					}
				}

				str = str.reverse();
			} else {
				var curMask = mask.charAt(str.length);
				for(i = 1; curMask != "#" && curMask != "*" && curMask != "$" && curMask != "%"; i++){
					if(origStr.length + i > mask.length){
						element.value = str;
						syslib.event.stopPropagation(evt);
						return;
					} else {
						str += curMask;
						curMask = mask.charAt(str.length + 1);
					}
				}
			}

			var regex = syslib.ui.MaskInput.regex[curMask];
			if(!regex.test(ch) || str.length >= mask.length){
				syslib.event.stopPropagation(evt);
				return false;
			}

			element.value = str;
		}
	}
});

// static functions
syslib.ui.MaskInput.regex = {
	"#" : /[0-9]/,
	"$" : /[a-zA-Z0-9wy]/,
	"%" : /\D/,
	"*" : /./
};
syslib.ui.MaskInput.isSpecialCode = function(code){
	var special = false;
	var scodes 	= [8, 13, 36, 35, 46, 0, 45, 33, 34, 9, 16, 17, 18];

	for(var i in scodes){
		var scode = scodes[i];
		if(scode == code){
			special = true;
		}
	}

	return special;
};